package com.example.baselibrary.db.dao;


import com.example.baselibrary.bean.UserManager;
import com.example.baselibrary.db.BTDBHelper;
import com.example.baselibrary.db.table.SearchHistoryTable;

import java.util.List;

import cc.vv.lklibrary.db.dao.DBBaseTableDao;
import cc.vv.lklibrary.db.help.DBBaseOpenHelper;
import cc.vv.lklibrary.log.LogOperate;

/**
 * @author Zml
 * @date 2018/5/15.
 */
public class SearchHistoryDao extends DBBaseTableDao<SearchHistoryTable, String> {

    public static final String TYPE_STR = "TYPE_STR";
    public static final String TYPE_GROUP = "TYPE_GROUP";
    public static final String TYPE_SINGLE = "TYPE_SINGLE";

    public SearchHistoryDao(DBBaseOpenHelper openHelper) {
        super(openHelper);
    }

    private static SearchHistoryDao mInstance;
    //TODO 筛选一周内数据

    public static SearchHistoryDao getInstance() {
        if (mInstance == null) {
            synchronized (SearchHistoryDao.class) {
                if (null == mInstance) {
                    mInstance = new SearchHistoryDao(BTDBHelper.getInstance());
                }
            }
        }
        return mInstance;
    }

    /**
     * 插入 or 更新数据
     *
     * @param searchHistoryTable
     */
    public void insertOrUpdate(SearchHistoryTable searchHistoryTable) {
        if (searchHistoryTable == null) {
            return;
        }
        try {
            List<SearchHistoryTable> searchHistoryTables = queryAllByType(searchHistoryTable.searchType);
            for (SearchHistoryTable ta :
                    searchHistoryTables) {
                if (ta != null) {
                    if (searchHistoryTable.keyword.equals(ta.keyword)) {
                        return;
                    }
                }
            }
            createOrUpdate(searchHistoryTable);
        } catch (Exception e) {
            LogOperate.e("搜索插入历史记录失败", e);
        }
    }

    /**
     * 查询当前账号所有历史记录
     *
     * @return
     */
    public List<SearchHistoryTable> queryAllRecord() {
        return queryByColumn("userId", UserManager.Companion.getUserId());
    }

    /**
     * 查某一类型的数据
     *
     * @return
     */
    public List<SearchHistoryTable> queryAllByType(String type) {
        return queryByColumnAnd("userId", UserManager.Companion.getUserId(), "searchType", type);
    }

    /**
     * 通过 (timestamp) 删除指定数据
     */
    public void deleteByTimestamp(String timestamp) {
        deleteByColumnAnd("userId", UserManager.Companion.getUserId(), "timestamp", timestamp);
    }

    /**
     * 清空当前账号所有类型的搜索记录
     */
    public void deletAllRecord() {
        deleteByColumn("userId", UserManager.Companion.getUserId());
    }

    /**
     * 删除某一类型的搜索记录
     *
     * @param type
     */
    public void deleteByType(String type) {
        deleteByColumnAnd("userId", UserManager.Companion.getUserId(), "searchType", type);
    }

    /**
     * 清除过期数据(7天之前的)
     */
    public void deletePastDueData() {
        long currentTime = System.currentTimeMillis();
        long sevenDay = 7 * 24 * 60 * 60 * 1000;
        List<SearchHistoryTable> allData = queryAllRecord();
        for (SearchHistoryTable data : allData) {
            if ((currentTime - Long.parseLong(data.timestamp)) > sevenDay) {
                // 删除数据库7天之前的数据
                delete(data);
            }
        }
    }
}
